﻿using Common;
using DevExpress.XtraEditors;
using DLL;
using NursingDocumentInfoSystem;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace NursingFormSystem.InfantBaby
{
    public partial class AddInfantBaby : Form
    {
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id = string.Empty;
        public AddInfantBaby(Tb_User loginUser, PatientInfo patientInfo, string Id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = Id;

            //加载电签
            ElectronicSignature();

            //绑定数据
            BindControl();
            BindInfo(Id);
        }
        public AddInfantBaby(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;

            //加载电签
            ElectronicSignature();

            //绑定数据
            BindControl();
            BindInfo(this.Id);
        }
        public AddInfantBaby(string[] loginUser, string[] patientInfo, string Id)
        {
            InitializeComponent();
            PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = Id;

            //加载电签
            ElectronicSignature();

            //绑定数据
            BindControl();
            BindInfo(Id);
        }

        private void BindControl()
        {
            foreach (var c in groupControl1.Controls)
            {
                if (c is TextEdit)
                    ((TextEdit)c).KeyUp += EnterTab_KeyUp;
            }
        }
        private void ElectronicSignature()
        {
            ElectronicSignature es = new ElectronicSignature(LoginUser);
            es.SearchElectronicSignatureImage(Base.ESSwitch, LoginUser, texthushiqianming, picElectronicSignature2, texthushiqianming.Location);
        }

        #region 显示赋值
        public void BindInfo(string Id)
        {
            texthushiqianming.EditValue = LoginUser.UserName;
            dateriqi.DateTime = DateTime.Now; //默认显示当前日期
            timeshijian.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Byays ps = new Byays();
            ps = XmlUtil.XMLToObject<Byays>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(ps.RiQi))
                dateriqi.DateTime = Convert.ToDateTime(ps.RiQi);//日期
            timeshijian.Text = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);//时间
            texttiwen.Text = ps.YiBanQingKuangTiWen;//体温
            cmbFanying.Text = ps.YiBanQingKuangFanYing;//反应
            cmbFuSe.Text = ps.YiBanQingKuangFuSe;//肤色
            cmbChunMuRu.Text = ps.WeiYangChunMuRu;//纯母乳
            textpeifangnai.Text = ps.WeiYangPeiFangNai;//配方奶
            textdabiancishu.Text = ps.DaBianCiShu;//大便次数
            comboDaBianYanSe.Text = ps.DaBianYanSe;   //大便颜色
            textxioabaincishu.Text = ps.XiaoBianCiShu;//小便次数
            textoutu.Text = ps.OuTuXingZhi;//呕吐性质
            cmbOuTuLiang.Text = ps.OuTuLiang;//呕吐量
            cmbJiDai.Text = ps.QiDai;//脐带
            cmbKuSheng.Text = ps.KuSheng;//哭声
            memoyingerqingkuangjichuli.Text = ps.YingErQingKuangJiChuLi;//婴儿情况及处理
            texthushiqianming.Text = ps.HuShiQianMing;//护士签名
        }
        #endregion

        #region 保存
        private void simpleButtonbaocun_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(dateriqi.Text.Trim()))
                dateriqi.DateTime = DateTime.Now;
            //时间判断
            if (!timeshijian.Text.Contains(":"))
            {
                timeshijian.Focus();
                MessageBox.Show("时间需mm:ss格式，或使用双击显示当前时间");
                return;
            }

            Byays fdd = new Byays();
            //日期
            fdd.RiQi = dateriqi.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //时间
            fdd.ShiJian = DateTimeTran.TimeFillZero(timeshijian.Text);

            //体温
            fdd.YiBanQingKuangTiWen = texttiwen.Text;
            //反应
            fdd.YiBanQingKuangFanYing = cmbFanying.Text;
            //肤色
            fdd.YiBanQingKuangFuSe = cmbFuSe.Text;
            //纯母乳
            fdd.WeiYangChunMuRu = cmbChunMuRu.Text;
            //配方奶
            fdd.WeiYangPeiFangNai = textpeifangnai.Text;
            //大便次数
            fdd.DaBianCiShu = textdabiancishu.Text;
            //大便颜色
            fdd.DaBianYanSe = comboDaBianYanSe.Text;
            //小便次数
            fdd.XiaoBianCiShu = textxioabaincishu.Text;
            // 呕吐性质
            fdd.OuTuXingZhi = textoutu.Text;
            //呕吐量
            fdd.OuTuLiang = cmbOuTuLiang.Text;
            //脐带
            fdd.QiDai = cmbJiDai.Text;

            //哭声
            fdd.KuSheng = cmbKuSheng.Text;
            //婴儿情况及处理
            fdd.YingErQingKuangJiChuLi = memoyingerqingkuangjichuli.Text;

            //签名
            if (string.IsNullOrEmpty(this.Id))
                fdd.HuShiQianMing = LoginUser.UserName;
            else
                fdd.HuShiQianMing = texthushiqianming.Text;

            //表单ID
            fdd.FormId = (int)FormType.BybyAndBaby;
            //表单名字
            fdd.FormName = EnumHelper.GetDescription(FormType.BybyAndBaby);
            //表单ID
            record.FormId = (int)FormType.BybyAndBaby;
            //表单名字
            record.FormName = EnumHelper.GetDescription(FormType.BybyAndBaby);
            //住院号
            record.PatientId = this.PatientInfo.PATIENT_ID;
            //住院次数
            record.VisitId = this.PatientInfo.VISIT_ID;
            //患者姓名
            record.PatientName = this.PatientInfo.PATIENT_NAME;
            //床号
            record.BedNo = PatientInfo.BED_NO;
            //修正转出 出院的问题
            if (string.IsNullOrEmpty(this.Id))
            {
                //当前患者科室ID
                record.DepartmentId = LoginUser.DepartmentCode;
                //当前患者科室名称
                record.DepartmentName = PatientInfo.DEPARTMENT_NAME;
                //当前患者病区ID
                record.AreaId = PatientInfo.NURSE_CELL_CODE;
                //当前患者病区名称
                record.AreaName = PatientInfo.NURSE_CELL_NAME;
            }
            //时间
            record.AssessmentDate = dateriqi.DateTime;
            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);

            record.NursingFormData = XmlUtil.ObjectToXML(fdd, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                if (Base.ESSwitch.Equals("on"))//电签开始
                {
                    string extraInfo = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><root><sign_sys>{0}</sign_sys><title>{1}</title><patient_id>{2}</patient_id><patient_name>{3}</patient_name><emr_id>{4}</emr_id><emr_catalog>{5}</emr_catalog><his_id>{6}</his_id><event_no>{7}</event_no><visit_number>{8}</visit_number><dept_name>{9}</dept_name><prescribe_number>{10}</prescribe_number><prescribe_sub_number>{11}</prescribe_sub_number><prescribe_operation>医嘱操作（发送、校对、停止、作废）或门诊处方操作（发送）</prescribe_operation><radiotherapy_id>放疗号</radiotherapy_id><process_name>CT定位申请单审核</process_name><process_time>CT定位申请单审核时间</process_time><extend_value_1>备用扩展1</extend_value_1><extend_value_2>备用扩展2</extend_value_2><extend_value_3>备用扩展3</extend_value_3><extend_value_4>备用扩展4</extend_value_4><extend_value_5>备用扩展5</extend_value_5><extend_value_6>备用扩展6</extend_value_6><extend_value_7>备用扩展7</extend_value_7><extend_value_8>备用扩展8</extend_value_8><extend_value_9>备用扩展9</extend_value_9></root>";
                    extraInfo = string.Format(extraInfo, EnumHelper.GetDescription(FormType.BybyAndBaby) + (record.Id > 0 ? "-更新" : "-新增"), EnumHelper.GetDescription(FormType.BybyAndBaby), record.PatientId, record.PatientName, record.Id, record.FormId, "", record.PatientId, record.VisitId, LoginUser.DepartmentName, record.PatientId, record.PatientId);
                    ElectronicSignature es = new ElectronicSignature(LoginUser);
                    bool res = es.GetElectronicSignatureImage(es, record.NursingFormData, extraInfo, record.FormId.ToString(), record.FormName, PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID);
                    if (!res) { MessageBox.Show("电子签名失败"); return; }
                }//电签结束                
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }

        }
        #endregion

        #region 取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

        }
        #endregion

        private void timeshijian_DoubleClick_1(object sender, EventArgs e)
        {
            this.timeshijian.Text = DateTime.Now.ToString("HH:mm");
        }

        void EnterTab_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnGetTempData_Click(object sender, EventArgs e)
        {
            if (this.PatientInfo == null) return;
            //导入体温相关
            TemperatureIn x = new TemperatureIn(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID.ToString());
            x.ShowDialog();
            if (x.DialogResult == DialogResult.Yes)
            {
                texttiwen.Text = x.tiwen;
            }
        }
    }
}